package com.sky.proxytest;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyApp {
    public static void main(String[] args) {
//        通过jdk动态代理，为目标对象产生代理对象，进行增强
        MyInterfacelmpl target = new MyInterfacelmpl(); // 目标对象

//        创建代理对象
            MyInterface proxy = (MyInterface) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    System.out.println("在代理对象中开启事务...");
                    Object invoke = method.invoke(target,args);
                    System.out.println("在代理对象中提交事务...");
                    return invoke;
                }
            });

            proxy.say();
    }
}
